//
// Created by MI T on 2024/6/18.
//

#ifndef UVCCAMERA_LOG_H
#define UVCCAMERA_LOG_H
#include "hilog/log.h"

#define free_napi(str) { if(str) { delete[] str; (str) = NULL;} }


// 全局的开关
extern bool DEBUG_ENABLE;
// 定义日志输出
#define LOG_D(...)                                                                                                     \
    do {                                                                                                               \
        if (DEBUG_ENABLE)                                                                                              \
            OH_LOG_Print(LOG_APP, LOG_DEBUG, 0x00101, "uvc_camera", __VA_ARGS__);                                      \
    } while (0)
#define LOG_W(...)                                                                                                     \
    do {                                                                                                               \
        if (DEBUG_ENABLE)                                                                                              \
            OH_LOG_Print(LOG_APP, LOG_WARN, 0x00101, "uvc_camera", __VA_ARGS__);                                       \
    } while (0)
#define LOG_I(...)                                                                                                     \
    do {                                                                                                               \
        if (DEBUG_ENABLE)                                                                                              \
            OH_LOG_Print(LOG_APP, LOG_INFO, 0x00101, "uvc_camera", __VA_ARGS__);                                       \
    } while (0)
#define LOG_E(...)                                                                                                     \
    do {                                                                                                               \
        if (DEBUG_ENABLE)                                                                                              \
            OH_LOG_Print(LOG_APP, LOG_ERROR, 0x00101, "uvc_camera", __VA_ARGS__);                                      \
    } while (0)

#define SAFE_FREE(p)                                                                                                   \
    {                                                                                                                  \
        if (p) {                                                                                                       \
            free((p));                                                                                                 \
            (p) = NULL;                                                                                                \
        }                                                                                                              \
    }
#define SAFE_DELETE(p)                                                                                                 \
    {                                                                                                                  \
        if (p) {                                                                                                       \
            delete (p);                                                                                                \
            (p) = NULL;                                                                                                \
        }                                                                                                              \
    }
#define SAFE_DELETE_ARRAY(p)                                                                                           \
    {                                                                                                                  \
        if (p) {                                                                                                       \
            delete[] (p);                                                                                              \
            (p) = NULL;                                                                                                \
        }                                                                                                              \
    }
#define NUM_ARRAY_ELEMENTS(p) ((int)sizeof(p) / sizeof(p[0]))

#if defined(__GNUC__)
// the macro for branch prediction optimaization for gcc(-O2/-O3 required)
#define CONDITION(cond) ((__builtin_expect((cond) != 0, 0)))
#define LIKELY(x) ((__builtin_expect(!!(x), 1)))   // x is likely true
#define UNLIKELY(x) ((__builtin_expect(!!(x), 0))) // x is likely false
#else
#define CONDITION(cond) ((cond))
#define LIKELY(x) ((x))
#define UNLIKELY(x) ((x))
#endif

#endif // UVCCAMERA_LOG_H
